package cn.wolfcode.luowowo.article.service.impl;

import cn.wolfcode.luowowo.article.domain.*;
import cn.wolfcode.luowowo.article.mapper.StrategyContentMapper;
import cn.wolfcode.luowowo.article.mapper.StrategyDetailMapper;
import cn.wolfcode.luowowo.article.query.StrategyDetailQuery;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.article.service.IStrategyDetailService;
import cn.wolfcode.luowowo.article.service.IStrategyService;
import cn.wolfcode.luowowo.article.service.IStrategyTagService;
import cn.wolfcode.luowowo.article.vo.StrategyPersistenceVo;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class StrategyDetailServiceImpl implements IStrategyDetailService {

    @Autowired
    private StrategyDetailMapper strategyDetailMapper;

    @Autowired
    private StrategyContentMapper strategyContentMapper;

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IDestinationService destinationService;

    @Autowired
    private IStrategyTagService strategyTagService;

    @Override
    public PageInfo query(StrategyDetailQuery qo) {
        PageHelper.startPage( qo.getCurrentPage(), qo.getPageSize() );
        return new PageInfo( strategyDetailMapper.selectForList( qo ) );
    }

    @Override
    public List <StrategyDetail> list() {
        return strategyDetailMapper.selectAll();
    }

    @Override
    public StrategyDetail get(Long id) {
        return strategyDetailMapper.selectByPrimaryKey( id );
    }

    @Override
    public void saveOrUpdate(StrategyDetail strategyDetail, String tags) {
        //目的地
        Strategy strategy = strategyService.selectByStrategyId( strategyDetail.getStrategy().getId() );
        Destination dest = strategy.getDest();
        strategyDetail.setDest( dest );
        //设置概要
        String content = strategyDetail.getStrategyContent().getContent();
        if (content != null && content.length() > 200) {
            strategyDetail.setSummary( content.substring( 0, 200 ) );
        } else {
            strategyDetail.setSummary( content );
        }
        //是否是国内
        List <Destination> toasts = destinationService.getToasts( dest.getId() );
        Long id = toasts.get( 0 ).getId();
        if (id == 1) {
            strategyDetail.setIsabroad( false );
        } else {
            strategyDetail.setIsabroad( true );
        }
        //添加操作
        if(strategyDetail.getId()==null){
            strategyDetailMapper.insert( strategyDetail );
            strategyDetail.setCreateTime( new Date() );
            strategyDetail.getStrategyContent().setId( strategyDetail.getId() );
            strategyContentMapper.insert( strategyDetail.getStrategyContent() );
        }else {
            strategyDetailMapper.updateByPrimaryKey( strategyDetail );
            //解除关系
            strategyDetailMapper.removeRelation(strategyDetail.getId());
        }
        //标签的处理
        StrategyTag tag = null;
        String[] ts = tags.split( "," );
        for (String t : ts) {
            //判断t是否存在
            tag = strategyTagService.selectByTagName( t );
            if (tag == null) {
                tag = new StrategyTag();
                tag.setName( t );
                strategyTagService.insert( tag );
            }
            strategyDetailMapper.inserRelation( tag.getId(), strategyDetail.getId() );
        }

    }

    @Override
    public StrategyContent queryContent(Long id) {
        StrategyContent strategyContent = strategyContentMapper.selectByPrimaryKey( id );
        return strategyContent;
    }

    @Override
    public List <StrategyDetail> selectViewNumTop3(Long destId) {
        List <StrategyDetail> list=  strategyDetailMapper.selectViewNumTop3(destId);
        return list;

    }

    @Override
    public void updateStatisVo(StrategyPersistenceVo spvo) {
            strategyDetailMapper.updateStatisVo(spvo);
    }

    @Override
    public List<StrategyDetail> queryStrategy(List<Long> strategyIds) {
        return strategyDetailMapper.selectSrtrategy(strategyIds);
    }

}
